#include "optiondialog.h"
#include "ui_optiondialog.h"

optionDialog::optionDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::optionDialog) {
    ui->setupUi(this);
    QSettings ini(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat, this);

    //QStringList m_serialPortName;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        ui->serialName->addItem(info.portName());
    }

    ui->connectType->setCurrentIndex(ini.value("network/type").toInt());
    ui->serialName->setCurrentText(ini.value("network/serial").toString());
    ui->baudRate->setCurrentText(ini.value("network/baudrate").toString());
    ui->hostEdit->setText(ini.value("network/host").toString());
    ui->portEdit->setValue(ini.value("network/port").toInt());
    ui->serverEdit->setValue(ini.value("device/server").toInt());
    ui->readAddress->setValue(ini.value("device/register").toInt());
    ui->pollTimeEdit->setValue(ini.value("device/polltime").toInt());
    set_max_col = ini.value("sys/col", 4).toInt();
    set_max_row = ini.value("sys/row", 2).toInt();

    standItemModel = new QStandardItemModel();

    //添加表头
    standItemModel->setColumnCount(3);
    standItemModel->setRowCount(set_max_col * set_max_row);
    standItemModel->setHeaderData(0, Qt::Horizontal, QStringLiteral("探测器号")); //设置表头内容
    standItemModel->setHeaderData(1, Qt::Horizontal, QStringLiteral("描述"));
    standItemModel->setHeaderData(2, Qt::Horizontal, QStringLiteral("是否启用"));
    for(int i = 1; i <= set_max_col * set_max_row; i++) {
        standItemModel->setItem(i - 1, 0, new QStandardItem(QString::number(i) + "#探测器"));
        standItemModel->setItem(i - 1, 1, new QStandardItem(ini.value(QString("device/describe%1").arg(i), "未使用").toString()));
        standItemModel->setItem(i - 1, 2, new QStandardItem(ini.value(QString("device/used%1").arg(i), 0).toString()));
        standItemModel->item(i - 1, 0)->setFlags((Qt::NoItemFlags)); //只读
    }
    ui->tableView->setModel(standItemModel);
    ui->tableView->setColumnWidth(0, 70);
    ui->tableView->setColumnWidth(1, 100);
    ui->tableView->setColumnWidth(2, 70);

}

optionDialog::~optionDialog() {
    delete ui;
}

void optionDialog::on_btnOk_clicked() {
    QSettings ini(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat, this);
    ini.setValue("network/type", ui->connectType->currentIndex());
    ini.setValue("network/serial", ui->serialName->currentText());
    ini.setValue("network/baudrate", ui->baudRate->currentText());
    ini.setValue("network/host", ui->hostEdit->text());
    ini.setValue("network/port", ui->portEdit->value());
    ini.setValue("device/server", ui->serverEdit->value());
    ini.setValue("device/register", ui->readAddress->value());
    ini.setValue("device/polltime", ui->pollTimeEdit->value());

    for(int i = 1; i <= set_max_col * set_max_row; i++) {
        ini.setValue(QString("device/describe%1").arg(i), standItemModel->item(i - 1, 1)->text());
        ini.setValue(QString("device/used%1").arg(i), standItemModel->item(i - 1, 2)->text());
    }

    emit flushData();
    QMessageBox::information(this, "提示信息", "新设置在重新启动程序后生效！");
    this->close();
}


void optionDialog::on_btnCancel_clicked() {
    this->close();
}


void optionDialog::on_connectType_currentIndexChanged(int index)
{
    if(index == 0) {
        ui->hostEdit->setEnabled(false);
        ui->portEdit->setEnabled(false);
        ui->baudRate->setEnabled(true);
        ui->serialName->setEnabled(true);
    } else {
        ui->hostEdit->setEnabled(true);
        ui->portEdit->setEnabled(true);
        ui->baudRate->setEnabled(false);
        ui->serialName->setEnabled(false);
    }
}

